{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 学习绘制分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://plot.ly/python/distplot/\n",
    "\n",
    "import plotly as py\n",
    "import plotly.graph_objs as go\n",
    "import plotly.figure_factory as ff\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\", message=\"numpy.dtype size changed\")\n",
    "warnings.filterwarnings(\"ignore\", message=\"numpy.ufunc size changed\")\n",
    "\n",
    "py.offline.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic distplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xs = np.random.randn(1000) # ndarray\n",
    "xs[:5], xs[-5:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# create_distplot 绘制多维度的分布, 所以都要加[]\n",
    "fig = ff.create_distplot(\n",
    "    hist_data=[xs],\n",
    "    group_labels=['basic distplot'],\n",
    "    bin_size=0.08,\n",
    "    colors=['#DAA520'],\n",
    ")\n",
    "\n",
    "# 曲线不是正态的密度函数图, 只是近似\n",
    "py.offline.iplot(fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ff.create_xx 和 go.figure区别, 就是ff.create_xx更方便了, go.figure更灵活\n",
    "# 箱线图, 需要使用go\n",
    "\n",
    "box_0 = go.Box(\n",
    "    x=xs,\n",
    "    name='box-name',\n",
    "    boxpoints='outliers',\n",
    "    jitter=1,\n",
    "    marker=dict( \n",
    "        color='#DAA520',\n",
    "    )\n",
    ")\n",
    "\n",
    "type(box_0) # go._box.Box\n",
    "\n",
    "layout = go.Layout(\n",
    "    title='layout-title',\n",
    "    width=600,\n",
    "    height=500,\n",
    "    yaxis=dict(\n",
    "        title='yaxis-title',\n",
    "        zeroline=True\n",
    "    )\n",
    ")\n",
    "\n",
    "fig = go.Figure(data=[box_0], layout=layout)\n",
    "\n",
    "py.offline.iplot(fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 小提琴, 不需要使用go, 优美的看到正态分布\n",
    "fig = ff.create_violin(data=xs, colors='#DAA520', title='violin-title')\n",
    "py.offline.iplot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot multiple datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xs_1 = np.random.randn(1000) - 2\n",
    "xs_2 = np.random.randn(1000) \n",
    "xs_3 = np.random.randn(1000) + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "fig = ff.create_distplot(\n",
    "    hist_data=[xs_1, xs_2, xs_3],\n",
    "    group_labels=['g1', 'g2', 'g3'],\n",
    "    bin_size=0.08,\n",
    "    curve_type='normal',\n",
    "    show_hist=False,\n",
    "    colors=['#FF0000', '#00FF00', '#0000FF'],\n",
    ")\n",
    "\n",
    "py.offline.iplot(fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
